/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.ParameterValue;
import model.solver.MiniSatParameter;

/**
 * A {@link IGDialogParameter} for {@link MiniSatParameter}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGMinisatDialogParameter extends IGDialogParameter {


	private static final long	serialVersionUID			= 1L;

	public static final String	PRE_DESCRIPTION				= "Activate preprocessor: ";
	public static final String	VERBOSITY_DESCRIPTION		= "Verbosity level: ";
	public static final String	DECAY_DESCRIPTION			= "Decay: ";
	public static final String	RND_DESCRIPTION				= "Rnd freq: ";
	public static final String	GROW_DESCRIPTION			= "Grow value: ";
	public static final String	RCHECK_DESCRIPTION			= "Rcheck value: ";
	public static final String	POLARITY_MODE_DESCRIPTION	= "Polarity mode: ";


	// public static final String TIMEOUT_BUTTON = "Set timeout";


	public static final String	YES							= "YES";
	public static final String	NO							= "NO";
	public static final String	NOT_USED					= "NOT USED";


	private MiniSatParameter	param;

	// private JButton timeoutButton;
	private JPanel				timeoutPanel;
	private JLabel				timeoutDescritpion;

	private JPanel				prePanel;
	private JLabel				preDescritpion;
	private JComboBox			preBox;

	private JPanel				verbosityPanel;
	private JLabel				verbosityDescritpion;
	private JComboBox			verbosityBox;

	private JPanel				decayPanel;
	private JLabel				decayDescritpion;
	private JTextField			decayField;

	private JPanel				rndPanel;
	private JLabel				rndDescritpion;
	private JTextField			rndField;

	private JPanel				growPanel;
	private JLabel				growDescritpion;
	private JTextField			growField;

	private JPanel				rcheckPanel;
	private JLabel				rcheckDescritpion;
	private JComboBox			rcheckBox;

	private JPanel				polarityModePanel;
	private JLabel				polarityModeDescritpion;
	private JComboBox			polarityModeBox;

	/**
	 * Create a {@link IGMinisatDialogParameter}.
	 * 
	 * @param param
	 *            {@link MiniSatParameter} of the solver
	 */
	public IGMinisatDialogParameter(MiniSatParameter param) {
		this.param = param;

		setLayout(new GridLayout(0, 1));

		// timeout
		timeoutPanel = new JPanel(new GridLayout(1, 2));
		// timeoutButton = new JButton(TIMEOUT_BUTTON);
		// timeoutButton.addActionListener(this);
		timeoutDescritpion = new JLabel(TIMEOUT_DESCRIPTION);
		timeoutPanel.add(timeoutDescritpion);
		timeoutPanel.add(timeoutField);
		// panel.add(timeoutButton);
		// panel.setBorder(BorderFactory.createTitledBorder(TIMEOUT_DESCRIPTION));

		add(timeoutPanel);

		// pre
		prePanel = new JPanel(new GridLayout(1, 2));
		preDescritpion = new JLabel(PRE_DESCRIPTION);
		preBox = new JComboBox();
		preBox.addItem(YES);
		preBox.addItem(NO);

		prePanel.add(preDescritpion);
		prePanel.add(preBox);

		add(prePanel);

		// verbosity
		verbosityPanel = new JPanel(new GridLayout(1, 2));
		verbosityDescritpion = new JLabel(VERBOSITY_DESCRIPTION);
		verbosityBox = new JComboBox();
		verbosityBox.addItem(NOT_USED);
		for (int i = MiniSatParameter.MIN_VERBOSITY ; i <= MiniSatParameter.MAX_VERBOSITY ; i++)
			verbosityBox.addItem(i);

		verbosityPanel.add(verbosityDescritpion);
		verbosityPanel.add(verbosityBox);

		add(verbosityPanel);

		// decay
		decayPanel = new JPanel(new GridLayout(1, 2));
		decayDescritpion = new JLabel(DECAY_DESCRIPTION);
		decayField = new JTextField();

		decayPanel.add(decayDescritpion);
		decayPanel.add(decayField);

		add(decayPanel);

		// rndfreq
		rndPanel = new JPanel(new GridLayout(1, 2));
		rndDescritpion = new JLabel(RND_DESCRIPTION);
		rndField = new JTextField();

		rndPanel.add(rndDescritpion);
		rndPanel.add(rndField);

		add(rndPanel);

		// grow
		growPanel = new JPanel(new GridLayout(1, 2));
		growDescritpion = new JLabel(GROW_DESCRIPTION);
		growField = new JTextField();

		growPanel.add(growDescritpion);
		growPanel.add(growField);

		add(growPanel);


		// rcheck
		rcheckPanel = new JPanel(new GridLayout(1, 2));
		rcheckDescritpion = new JLabel(RCHECK_DESCRIPTION);
		rcheckBox = new JComboBox();
		for (ParameterValue v : ParameterValue.values())
			rcheckBox.addItem(v);

		rcheckPanel.add(rcheckDescritpion);
		rcheckPanel.add(rcheckBox);

		add(rcheckPanel);


		// polarityMode
		polarityModePanel = new JPanel(new GridLayout(1, 2));
		polarityModeDescritpion = new JLabel(POLARITY_MODE_DESCRIPTION);
		polarityModeBox = new JComboBox();
		for (ParameterValue v : ParameterValue.values())
			polarityModeBox.addItem(v);

		polarityModePanel.add(polarityModeDescritpion);
		polarityModePanel.add(polarityModeBox);

		add(polarityModePanel);


		refresh();
	}

	@Override
	public void refresh() {
		timeoutField.setText("" + param.getTimeOut());

		if (param.isPre())
			preBox.setSelectedItem(YES);
		else
			preBox.setSelectedItem(NO);

		if (param.getVerbosity() == MiniSatParameter.NOT_USED)
			verbosityBox.setSelectedItem(NOT_USED);
		else
			verbosityBox.setSelectedItem(param.getVerbosity());

		polarityModeBox.setSelectedItem(param.getPolarityMode());

		rcheckBox.setSelectedItem(param.getRcheck());

		if (param.getGrow() == MiniSatParameter.NOT_USED)
			growField.setText(NOT_USED);
		else
			growField.setText("" + param.getGrow());

		if (param.getRndfreq() == MiniSatParameter.NOT_USED)
			rndField.setText(NOT_USED);
		else
			rndField.setText("" + param.getRndfreq());

		if (param.getDecay() == MiniSatParameter.NOT_USED)
			decayField.setText(NOT_USED);
		else
			decayField.setText("" + param.getDecay());


	}

	@Override
	public boolean checkAndSetParameter() {
		boolean good = true;

		// timeout
		String timeout = timeoutField.getText();
		if (timeout.isEmpty())
		{
			param.setTimeOut(MiniSatParameter.NO_DELAY);
			timeoutPanel.setBorder(null);
		}
		else
			try
			{
				param.setTimeOut(Integer.parseInt(timeout));
				timeoutPanel.setBorder(null);
			} catch (Exception e)
			{
				timeoutPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}

		// pre
		if (preBox.getSelectedItem().equals(YES))
			param.setPre(true);
		else
			param.setPre(false);

		// rcheq
		if (!param.setRcheck((ParameterValue) rcheckBox.getSelectedItem()))
		{
			rcheckPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}
		else
			rcheckPanel.setBorder(null);

		// grow
		String grow = growField.getText();
		if (grow.isEmpty() || grow.equals(NOT_USED))
			grow = "" + MiniSatParameter.NOT_USED;
		try
		{
			if (!param.setGrow(Integer.parseInt(grow)))
			{
				growPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}
			else
				growPanel.setBorder(null);

		} catch (Exception e)
		{
			growPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}

		// polaritymode
		param.setPolarityMode((ParameterValue) polarityModeBox.getSelectedItem());

		// decay
		String decay = decayField.getText();
		if (decay.isEmpty() || decay.equals(NOT_USED))
			decay = "" + MiniSatParameter.NOT_USED;

		try
		{
			if (!param.setDecay(Double.parseDouble(decay)))
			{
				decayPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}
			else
				decayPanel.setBorder(null);
		} catch (Exception e)
		{
			decayPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}

		// rndfreq
		String rnd = rndField.getText();
		if (rnd.isEmpty() || rnd.equals(NOT_USED))
			rnd = "" + MiniSatParameter.NOT_USED;
		try
		{
			if (!param.setRndfreq(Double.parseDouble(rnd)))
			{
				rndPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}
			else
				rndPanel.setBorder(null);
		} catch (Exception e)
		{
			rndPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}

		// verbosity
		if (((String) verbosityBox.getSelectedItem()).equals(NOT_USED))
			param.setVerbosity(MiniSatParameter.NOT_USED);
		else
			param.setVerbosity((Integer) verbosityBox.getSelectedItem());


		return good;
	}

}
